/******************************************************************************************************
Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/
/*global Application, Env, Page, NMRSpectrum, print, Molecule*/

var printHook = {
	fTemplate : { 'NMR Spectrum': {'background.opacity': 0,
					'grid.showhorizontal': false,
					'grid.showvertical': false,
					'curve.color': 'black',
					'curve.linewidth': 0,
					'axes.color': 'black',
					'integrals.curve.color': 'black',
					'integrals.label.color': 'black',
					'multiplets.fontcolor': 'black',
					'multiplets.integral.curve.color': 'black',
					'multiplets.integral.label.color': 'black',
					'peaks.color': 'black',
					'title.color': 'black'},
			'Molecule' : {'atomFontColor': 'black'} },

	fBackup : {},

	pageItems : function (aPagesIds) {
		'use strict';
		var page, i, activeDocument, items;

		items = [];
		activeDocument = Application.mainWindow.activeDocument;
		for (i = 0; i < aPagesIds.length; ++i) {
			page = new Page(activeDocument.page(aPagesIds[i]));
			items = items.concat(page.items);
		}
		return items;
	},

	updateItem : function (aItem) {
		'use strict';
		var id, name, props, p, backupData, nmrSpc, molecule;

		backupData = {};
		name = aItem.name;
		id = aItem.uuid;
		props = this.fTemplate[name];
		if (props !== undefined) {
			if (name === 'NMR Spectrum') {
				nmrSpc = new NMRSpectrum(aItem);
				if (nmrSpc.isValid()) {
					for (p in props) {
						if (props.hasOwnProperty(p)) {
							try {
								print(p + ' ' + nmrSpc.getProperty(p));
								backupData[p] =  nmrSpc.getProperty(p);
								nmrSpc.setProperty(p, props[p]);
								print(p + ' ' + nmrSpc.getProperty(p));
							} catch (er) {
								print(p + ' ' + er);
							}
						}
					}
					this.fBackup[id] = backupData;
				}
			} else if (name === 'Molecule') {
				molecule = new Molecule(aItem);
				if (molecule.isValid()) {
					this.fBackup[id] = molecule.graphicProperties();
					print(this.fBackup[id]);
					molecule.setGraphicProperties(props);
					print(molecule.graphicProperties());
				}
			}
			return true;
		}
		return false;
	},

	beforePrinting : function (aJSONString) {
		'use strict';
		var args, items, i, item, needsUpdate;

		print("Before Printing");
		print(aJSONString);
		args = JSON.parse(aJSONString);

		needsUpdate = false;
		items = this.pageItems(args.pageIds);
		for (i = 0; i < items.length; ++i) {
			item = items[i];
			needsUpdate = this.updateItem(item);
		}
		return true;
	},

	afterPrinting : function (aJSONArgs) {
		'use strict';
		var id, props, activeDocument, item, nmrSpc, p, molecule;

		print("After Printing");
		activeDocument = Application.mainWindow.activeDocument;
		for (id in this.fBackup) {
			if (this.fBackup.hasOwnProperty(id)) {
				item = activeDocument.getItem(id);
				if (item.isValid()) {
					props = this.fBackup[id];
					if (item.name === "NMR Spectrum") {
						nmrSpc = new NMRSpectrum(item);
						if (nmrSpc.isValid()) {
							for (p in props) {
								if (props.hasOwnProperty(p)) {
									try {
										print(p + ' ' + nmrSpc.getProperty(p));
										nmrSpc.setProperty(p, props[p]);
										print(p + ' ' + nmrSpc.getProperty(p));
									} catch (er) {
										print(p + ': ' + er);
									}
								}
							}
						}
					} else if (item.name === "Molecule") {
						molecule = new Molecule(item);
						if (molecule.isValid()) {
							print(molecule.graphicProperties());
							molecule.setGraphicProperties(props);
							print(molecule.graphicProperties());
						}
					}
				}
			}
		}
		return true;
	}
};

function printHookTest() {
	'use strict';
	var pages, activeDocument;

	activeDocument = Application.mainWindow.activeDocument;
	pages = JSON.stringify({'pageIds': activeDocument.pagesIds()});
	printHook.beforePrinting(pages);
	printHook.afterPrinting(pages);
}

Application.mainWindow.installEventHandler("beforePrinting", "printHook.beforePrinting");
Application.mainWindow.installEventHandler("afterPrinting", "printHook.afterPrinting");
